package com.lxb.myforum.controller.admin.sysmgt

import javax.servlet.http.HttpServletRequest

import com.lxb.myforum.base.BaseController
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

import com.lxb.myforum.domain.PageResponse
import com.lxb.myforum.pojo.Authority
import com.lxb.myforum.service.AuthorityService

/**
 *
 * @author lxb
 */
@Controller("adminAuthCtrl")
@RequestMapping("/admin/authorities")
class AuthorityController @Autowired
constructor(private val authorityService: AuthorityService) : BaseController() {

    @GetMapping(value = ["/", ""])
    fun index(): String = "/admin/sysmgt/authorities/index"

    /**
     *
     * @param map
     * @param pageable
     * 分页信息 page size
     * @return
     */
    @GetMapping(value = ["/all", "/list"])
    @ResponseBody
    fun authorities(page: Int, size: Int): PageResponse<Authority> {
        val rp = PageRequest.of(page - 1, size)
        val roles = authorityService.getAuthorities(rp)
        return PageResponse(0L, "", totalCount = roles.totalElements, rows = roles.content)
    }

    @RequestMapping(value = ["", "/"], method = [RequestMethod.PUT, RequestMethod.POST])
    @ResponseBody
    fun saveAuth(req: HttpServletRequest,
                 @ModelAttribute authority: Authority): Map<String, Any> {
        return authorityService.saveAuth(authority)
    }

    /**
     * 为权限删除资源
     * @param roleId
     * @param authIds
     * @return
     */
    @DeleteMapping(value = ["/{id}/res"])
    @ResponseBody
    fun delRes(@PathVariable("id") authId: Long,
               @RequestParam("resId[]") resIds: Set<Long>): Map<String, Any> {
        val result = authorityService.delAuthRes(authId, resIds)
        if (result["result"] as Int > 0) {
            result["ownRes"] = authorityService.findOwnRes(authId)
            result["notOwnRes"] = authorityService.findNotOwnRes(authId)
        }
        return result
    }

    /**
     * 为权限新增资源
     * @param roleId 角色ID
     * @param authIds 权限ID集合
     * @return
     */
    @PostMapping(value = ["/{id}/addRes"])
    @ResponseBody
    fun addRes(@PathVariable("id") authId: Long,
               @RequestParam("resId[]") resIds: Set<Long>): Map<String, Any> {
        val result = authorityService.addAuthRes(authId, resIds)
        if (result["result"] as Int > 0) {
            result["ownRes"] = authorityService.findOwnRes(authId)
            result["notOwnRes"] = authorityService.findNotOwnRes(authId)
        }
        return result
    }

    /**
     * 获取该角色相关的权限信息： 包括拥有什么权限、未拥有的权限
     */
    @GetMapping(value = ["/{id}/res"])
    fun getAuthorities(req: HttpServletRequest,
                       @PathVariable("id") authId: Long): String {
        req.setAttribute("ownRes", authorityService.findOwnRes(authId))
        req.setAttribute("notOwnRes", authorityService.findNotOwnRes(authId))
        return "/admin/sysmgt/authorities/authRes"
    }
}
